/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ifly.voice.app;

import android.app.Application;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.IBinder;

import java.lang.ref.WeakReference;

import com.ifly.voice.recognizer.RecognitionListener;
import com.ifly.voice.service.IVoiceListener;
import com.ifly.voice.service.VoiceService;
import com.lidroid.xutils.util.LogUtils;

public class App extends Application
{
	static
	{
		System.loadLibrary("pocketsphinx_jni");
	}

	private static App app;

	public static App getInstance()
	{
		return app;
	}

	private LoadModel mModel;
	private DicHelp dicHelp;

	@Override
	public void onCreate()
	{
		super.onCreate();

		app = this;
		mModel = new LoadModel(this);

		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_MEDIA_EJECT);
		filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		filter.addDataScheme("file");
		registerReceiver(mModel, filter);

		dicHelp = new DicHelp(this);
		dicHelp.startCopy();

		Intent service = new Intent(this, VoiceService.class);
		bindService(service, conn, BIND_AUTO_CREATE);

	}

	IVoiceListener iVoiceListener;

	public IVoiceListener getiVoiceListener()
	{
		return iVoiceListener;
	}

	private ServiceConnection conn = new ServiceConnection()
	{

		@Override
		public void onServiceDisconnected(ComponentName name)
		{
			iVoiceListener.stopListen();
			Logs.d("app  onServiceDisconnected ");
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service)
		{
			iVoiceListener = (IVoiceListener) service;
			Logs.d("app  onServiceConnected  iVoiceListener=" + iVoiceListener);
			iVoiceListener.startThread();
			if (recognitionListener != null)
			{
				iVoiceListener.setRecognitionListener(recognitionListener);
			}
			iVoiceListener.startListen();

		}
	};
	private RecognitionListener recognitionListener;

	@Override
	public void onTerminate()
	{
		kill();
		LogUtils.d("onTerminate");
		super.onTerminate();
	}

	public void kill()
	{
		unbindService(conn);
		unregisterReceiver(mModel);
		iVoiceListener = null;
		app = null;
	}

	public DicHelp getDicHelp()
	{
		return dicHelp;
	}

	public void firstInitListener(RecognitionListener recognitionListener)
	{
		this.recognitionListener = recognitionListener;
	}
	
}
